package cn.edu.sgu.www.mhxysy.controller.except;

import cn.edu.sgu.www.mhxysy.annotation.AnonymityAccess;
import cn.edu.sgu.www.mhxysy.config.property.CaptchaImageProperties;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.strategy.cache.CacheStrategy;
import cn.edu.sgu.www.mhxysy.util.HttpUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "验证码管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Slf4j
@RestController
@RequestMapping(path = "/captcha", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class CaptchaController {

    private final CacheStrategy redisCacheStrategy;
    private final CaptchaImageProperties captchaImageProperties;

    @Autowired
    public CaptchaController(
            CacheStrategy redisCacheStrategy,
            CaptchaImageProperties captchaImageProperties) {
        this.redisCacheStrategy = redisCacheStrategy;
        this.captchaImageProperties = captchaImageProperties;
    }

    @AnonymityAccess
    @ApiOperation("生成验证码")
    @RequestMapping(value = "/generate", method = RequestMethod.GET)
    public void generate(
            @ApiParam(name = "uuid", value = "验证码ID", required = true)
            @NotNull(message = "验证码" + CustomMessages.ID_NOT_NULL) String uuid) throws IOException {
        // 获取验证码配置
        Integer width = captchaImageProperties.getWidth();
        Integer height = captchaImageProperties.getHeight();
        Integer length = captchaImageProperties.getLength();

        // 创建验证码图片
        Captcha captcha = new SpecCaptcha(width, height);

        captcha.setLen(length);
        captcha.setCharType(Captcha.TYPE_DEFAULT);

        // 得到验证码的文本
        String code = captcha.text();

        // 将验证码缓存到Redis
        redisCacheStrategy.store(uuid, code);

        log.debug("本次生成的登录验证码：{}", code);

        // 获取HttpServletResponse对象
        HttpServletResponse response = HttpUtils.getResponse();

        // 设置请求头
        response.setContentType(MimeTypes.IMAGE_PNG);
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            // 输出图片流
            captcha.out(outputStream);
        }
    }

}